package dao;

import entity.Item;
import util.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * @Author Doris.Lee
 * @Description 商品的业务逻辑类
 * @Locate dao.ItemDao
 * @Datetime 2020-05-21 07:15
 * @Version 1.0
 */
public class ItemDao {

    //获得所有商品的信息
    public ArrayList<Item> getAllItems() {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null; //数据集
        ArrayList<Item> list = new ArrayList<>();// 商品集合

        //对数据库操作容易出现异常
        try {
            conn = DBHelper.getConnection();
            String sql = "select * from item";// SQL语句
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while (rs.next()) {
                Item item = new Item();
                item.setId(rs.getInt("id"));
                item.setName(rs.getString("name"));
                item.setCity(rs.getString("city"));
                item.setNumber(rs.getInt("number"));
                item.setPrice(rs.getInt("price"));
                item.setPicture(rs.getString("picture"));
                list.add(item); // 把一个商品加入集合
            }
            return list; // 返回集合。
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {  //释放资源
            //不要释放连接conn，因为是单例模式对象，释放了之后的方法都没办法使用。

            // 释放数据集对象rs
            if (rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

            // 释放语句对象
            if (stmt != null) {
                try {
                    stmt.close();
                    stmt = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    // 根据商品编号获得商品信息
    public Item getItemsById(int id) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBHelper.getConnection();
            String sql = "select * from item where id=?;"; // SQL语句：传一个id
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            rs = stmt.executeQuery();
            if (rs.next()) {
                Item item = new Item();
                item.setId(rs.getInt("id"));
                item.setName(rs.getString("name"));
                item.setCity(rs.getString("city"));
                item.setNumber(rs.getInt("number"));
                item.setPrice(rs.getInt("price"));
                item.setPicture(rs.getString("picture"));
                return item;
            } else {
                return null;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            // 释放数据集对象
            if (rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            // 释放语句对象
            if (stmt != null) {
                try {
                    stmt.close();
                    stmt = null;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

        }
    }

    //获取最近浏览的前五条商品信息
    public ArrayList<Item> getViewList(String list) {
        System.out.println("list:" + list);
        ArrayList<Item> itemList = new ArrayList<>();
        int iCount = 5; //每次返回前五条记录
        if (list != null && list.length() > 0) {
            String[] arr = list.split("#");

            //保存的是字符串类型，需要转换成整型
//V1：返回前五条记录，所以要判断，优化入v2
//            for (String s : arr) {
//                int id = Integer.parseInt(s);
//                itemList.add(getItemsById(id));
//            }
//            return itemList;

//V2
            if (arr.length >= 5) {
                for (int i = arr.length - 1; i >= arr.length - iCount; i--) {
                    itemList.add(getItemsById(Integer.parseInt(arr[i])));
                }
            } else {
                for (int i = arr.length - 1; i >= 0; i--) {
                    itemList.add(getItemsById(Integer.parseInt(arr[i])));
                }
            }
            return itemList;

        } else {
            return null;
        }
    }

}
